package main

import (
	"fmt"
	"golang.org/x/time/rate"
	"time"
)

func main() {
	const (
		speed = 1  //每秒执行的次数
		capacity = 5 //桶的容量大小
	)
	var gameScene = rate.NewLimiter(speed , capacity)
	for {
		if gameScene.AllowN(time.Now(), 2) {// 每次取2个
			// 业务代码
			fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
		} else {
			fmt.Println("too many request")
		}
		time.Sleep(time.Second)
	}
}
